package com.peng.leetcode.graph;

/**
 * FindJudge
 * 997. 找到小镇的法官
 *
 * @author: lupeng6
 * @create: 2021/1/6 18:28
 */
public class FindJudge {

    public static void main(String[] args) {
        int N = 2;
        int[][] trust = {{1, 2}};
        int judge = new Solution().findJudge(N, trust);
        System.out.println(judge);
    }

    static class Solution {
        public int findJudge(int N, int[][] trust) {
            int[] inDegree = new int[N + 1];
            int[] outDegree = new int[N + 1];
            for (int i = 0; i < trust.length; i++) {
                int[] arr = trust[i];
                outDegree[arr[0]]++;
                inDegree[arr[1]]++;
            }
            for (int i = 1; i <= N; i++) {
                int in = inDegree[i];
                int out = outDegree[i];
                if (in == N - 1 && out == 0) {
                    return i;
                }
            }
            return -1;
        }
    }
}
